<template>
  <div class="mod-config" style="margin-top: 30px">
    <div>
      <el-button
        type="primary"
        @click="checkPerson('sjkh')"
      >新增</el-button>

      <el-button
        type="primary"
        @click="deleteBatch"
      >删除</el-button>
    </div>


    <div v-if="showBlock">
      <el-table
        style="width: 100%; margin-top: 20px"
        :data="dataList"
        border
        v-show="tabType == 1"
        v-loading="dataListLoading"
        @selection-change="handleSelectionChange"
        ref="table2"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column
          type="index"
          width="80"
          header-align="center"
          align="center"
          label="序号"
        ></el-table-column>
        <el-table-column
          prop="ygbm"
          header-align="center"
          align="center"
          width="90"
          label="员工编号"
          show-overflow-tooltip
        >
         <!-- <template slot-scope="scope">
            <el-input v-model="scope.row.ygbh" placeholder="员工编号" />
          </template>-->
        </el-table-column>

        <el-table-column
          prop="ygname"
          header-align="center"
          align="center"
          width="120"
          label="考核对象"
          show-overflow-tooltip
        >
        <!--  <template slot-scope="scope">
            <el-input
              v-model="scope.row.name"
              placeholder="姓名"
            />
          </template>-->
        </el-table-column>
        <el-table-column
          prop="deptName"
          header-align="center"
          align="center"
          width="120"
          label="部门"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          v-if="show"
          prop="deptId"
          header-align="center"
          align="center"
          label="部门id"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="gwmc"
          header-align="center"
          align="center"
          width="120"
          label="岗位"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          v-if="show"
          prop="gwid"
          header-align="center"
          align="center"
          label="岗位id"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column align="center" label="测评">
          <el-table-column
            prop="cpScore"
            header-align="center"
            align="center"
            width="100"
            label="得分"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="cpQz"
            header-align="center"
            align="center"
            width="100"
            label="权重"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.cpQz"
                placeholder="权重"
                @input="addCpQz(scope.row, scope.$index)"
              />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="日常">
          <el-table-column
            prop="rcScore"
            header-align="center"
            align="center"
            width="100"
            label="得分"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="rcQz"
            header-align="center"
            align="center"
            width="100"
            label="权重"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.rcQz"
                placeholder="权重"
                @input="addRcQz(scope.row, scope.$index)"
              />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="合同">
          <el-table-column
            prop="htScore"
            header-align="center"
            align="center"
            width="100"
            label="得分"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="htQz"
            header-align="center"
            align="center"
            width="100"
            label="权重"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.htQz"
                placeholder="权重"
                @input="addHtQz(scope.row, scope.$index)"
              />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="重点任务">
          <el-table-column
            prop="rwScore"
            header-align="center"
            align="center"
            width="100"
            label="得分"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="rwQz"
            header-align="center"
            align="center"
            width="100"
            label="权重"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.rwQz"
                placeholder="权重"
                @input="addRwQz(scope.row, scope.$index)"
              />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="工作任务书">
          <el-table-column
            prop="gzScore"
            header-align="center"
            align="center"
            width="100"
            label="得分"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="gzQz"
            header-align="center"
            align="center"
            width="100"
            label="权重"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.gzQz"
                placeholder="权重"
                @input="addGzQz(scope.row, scope.$index)"
              />
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          prop="xs"
          header-align="center"
          align="center"
          width="120"
          label="系数"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.xs"
              placeholder="系数"
              @input="addXs(scope.row, scope.$index)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="zhdf"
          header-align="center"
          align="center"
          width="120"
          label="综合得分"
          show-overflow-tooltip
        >
        </el-table-column>

      </el-table>
    </div>
    <UserCommonSelect v-if="usercomment" ref="usercomment"  @refresh="refresh" :zid="zid"></UserCommonSelect>
  </div>
</template>

<script>

import UserCommonSelect from "@/views/statisticsanalysis/components/usercomment-select";//人员选择的组件
import {
    getLbSelect,
    getById,
    getKhdxList,
    putObj,
    deleteChildEvent
} from "@/api/dailyExamine/eventExamine/event.js";
import {
    getBmhzmxByZid
} from "@/api/statisticsanalysis/pfhzbmhzApi.js";

import { mapGetters, mapState } from "vuex";
import store from "@/store";
export default {

  components: {
      UserCommonSelect
  },
  props: {
      zid: String,
  },
  data() {
    return {
      headers: {
          Authorization: "Bearer " + store.getters.access_token
      },
      editFlag: 0,
      show:false,
      showBlock: true,
      dataForm: {},
      tabType: "1",
      radio: "1",
      dataList: [],
      dataListLoading: false,
      dialogVisible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      type: 0,
      index: 1,
      usercomment: false, //人员选择显隐
    };
  },
  computed: {
      ...mapGetters(["permissions"])

  },
  watch: {
      eventzxForm: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.showBlock = false;
          this.$nextTick(res=>{
            this.showBlock = true;
          })
        }
      },
      immediate: true,
      deep: true,
    },
  },
  created() {
      //this.getDataList();
  },
  methods: {
    init(row, type) {
      this.getDataList();
    },
    // 勾选数据
    handleSelectionChange(val) {
        this.multipleSelection = val;
    },

    selectSecondSelect(){
        //选择的时候强制刷新
        this.$forceUpdate()
    },
    //修改列表展示
    getList(data){
        this.editLock();
         data.map(it =>{
            if(it.yj != null && it.yj != ""){
                let item = {
                    name:it.yjms,
                    value:it.yj
                };
                it.yjsList = [];
                it.yjsList.push(item);
            }
        })
        this.dataList = data.slice(0,data.length);
    },
    //修改状态
    editLock() {
        this.editFlag = 1;
    },

    addCpQz(row, index){
        let data = [];
        data = this.$refs.table2.selection;
        if(data.length != 0){
            let  checked = [];
            let  inputId = row.id;
            data.map(ele => {
                checked.push(ele.id);
            });

            data.map(it => {

                if(checked.includes(inputId)){
                    it.cpQz = row.cpQz;

                }
            });
        }
    },
    addRcQz(row, index){
        let data = [];
        data = this.$refs.table2.selection;
        if(data.length != 0){
            let  checked = [];
            let  inputId = row.id;
            data.map(ele => {
                checked.push(ele.id);
            });

            data.map(it => {

                if(checked.includes(inputId)){
                    it.rcQz = row.rcQz;

                }
            });
        }
    },
    addHtQz(row, index){
        let data = [];
        data = this.$refs.table2.selection;
        if(data.length != 0){
            let  checked = [];
            let  inputId = row.id;
            data.map(ele => {
                checked.push(ele.id);
            });

            data.map(it => {

                if(checked.includes(inputId)){
                    it.htQz = row.htQz;

                }
            });
        }
    },
    addRwQz(row, index){
        let data = [];
        data = this.$refs.table2.selection;
        if(data.length != 0){
            let  checked = [];
            let  inputId = row.id;
            data.map(ele => {
                checked.push(ele.id);
            });

            data.map(it => {

                if(checked.includes(inputId)){
                    it.rwQz = row.rwQz;

                }
            });
        }
    },
    addGzQz(row, index){
        let data = [];
        data = this.$refs.table2.selection;
        if(data.length != 0){
            let  checked = [];
            let  inputId = row.id;
            data.map(ele => {
                checked.push(ele.id);
            });

            data.map(it => {

                if(checked.includes(inputId)){
                    it.gzQz = row.gzQz;

                }
            });
        }
    },
    addXs(row, index){
        let data = [];
        data = this.$refs.table2.selection;
        if(data.length != 0){
            let  checked = [];
            let  inputId = row.id;
            data.map(ele => {
                checked.push(ele.id);
            });

            data.map(it => {

                if(checked.includes(inputId)){
                    it.xs = row.xs;


                    if(it.cpQz == null || it.rcQz == null || it.htQz == null || it.rwQz == null || it.gzQz == null){

                        this.$message.warning("所有权重不能为空");
                        throw new Error('end') // 终止循环

                    }

                      let zf = Number(((it.cpScore*it.cpQz)+(it.rcScore*it.rcQz)+(it.htScore*it.htQz)+(it.rwScore*it.rwQz)+(it.gzScore*it.gzQz))*it.xs);
                      it.zhdf = this.numFilter(zf);

                }


            });
        }

        //两数组对象，获取不同的两数组对象

        let getArrayList = this.getArrDifSameValue(this.$refs.table2.selection,this.dataList)
        console.log(this.dataList)
        console.log(this.$refs.table2.selection)
        console.log(getArrayList);
        getArrayList.map(it => {
            if(it.id == row.id){
                if(it.cpQz == null || it.rcQz == null || it.htQz == null || it.rwQz == null || it.gzQz == null){

                    this.$message.warning("所有权重不能为空");
                    throw new Error('end') // 终止循环

                }

                let zf = Number(((it.cpScore*it.cpQz)+(it.rcScore*it.rcQz)+(it.htScore*it.htQz)+(it.rwScore*it.rwQz)+(it.gzScore*it.gzQz))*it.xs);
                it.zhdf = this.numFilter(zf);
            }
        })


    },
    getArrDifSameValue(arr1,arr2){
        var result = [];
        for(var i = 0; i < arr2.length; i++){
            var obj = arr2[i];
            var id = obj.id;
            var isExist = false;
            for(var j = 0; j < arr1.length; j++){
                var aj = arr1[j];
                var n = aj.id;
                if(n == id){
                    isExist = true;
                    break;
                }
            }
            if(!isExist){
                result.push(obj);
            }
        }
        return result;
    },
    numFilter(value) {
        const realVal = parseFloat(value).toFixed(1);
        return realVal;
    },
    //打开人员
    checkPerson(type) {
        if (type == "sjkh") {
            this.usercomment = true;
            this.$nextTick(() => {
                this.$refs.usercomment.init("12", type, JSON.stringify(this.dataList),"","htcj");  //fakeData
            });
        }

    },
    refresh(){//刷新
        this.getDataList();
    },
    /*//选取后列表
    submitfy(data) {

        let personList = data.tableDataUser.map(it => {
            return {
                cyrUserId: it.userId,
                cyrRealName: it.userName,
                cyrDeptId: it.deptId,
                cyrDeptName: it.deptName,
                cyrUserName: it.userCode
            };
        });

        personList.forEach((ele) => {
            var item = {
              id:this.index,
              ygbm:'',//员工编码
              ygname:'',//考核对象
              gwid:'',//岗位ID
              gwmc:'',//岗位名称
              deptId:'',//所属部门ID
              deptName:'',//部门名称
              cpScore:'',//测评得分
              cpQz:'',//测评权重
              rcScore:'',//日常得分
              rcQz:'',//日常权重
              htScore:'',//合同得分
              htQz:'',//合同权重
              rwScore:'',//任务得分
              rwQz:'',//任务权重
              gzScore:'',//工作任务书得分
              gzQz:'',//工作任务书权重
              xs:'',//系数
              zhdf:'',//综合得分

            };
            this.index++;
            this.dataList.push(item);

        });
    },*/
    //子组件列表
     getDataList() {
      this.dataList=[];
      this.dataListLoading = true;
      getBmhzmxByZid(this.zid).then(r => {

        this.dataList = r.data.data;

      });
      this.dataListLoading = false;

    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },


  },
};
</script>

<style>

</style>
